import { getEnabledElement } from "@cornerstonejs/core";
import * as cornerstoneTools from "@cornerstonejs/tools";
const { BaseTool } = cornerstoneTools;

// const defaultConfiguration = {
// 	sensitivity: 0.15,
// 	minZoom: 0.15,
// 	maxZoom: 20,
// 	invert: false
// };

// type ZoomConfiguration = typeof defaultConfiguration;

export default class ZoomMouseWheelTool extends BaseTool {
	static toolName;

	constructor(
		toolProps = {},
		defaultToolProps = {
			supportedInteractionTypes: ["Mouse", "Touch"],
			configuration: {
				factor: 0.1,
				minZoomScale: 0.001,
				maxZoomScale: 1000,
				invert: false
			}
		}
	) {
		super(toolProps, defaultToolProps);
	}

	mouseWheelCallback(evt) {
		const { element, wheel } = evt.detail;
		const enabledElement = getEnabledElement(element);
		if (enabledElement) {
			const viewport = enabledElement.viewport;

			const currentZoom = viewport.getZoom();
			const { factor, minZoomScale, maxZoomScale, invert } = this.configuration;
			const delta = factor * Math.abs(wheel.spinY);

			let newZoom = wheel.direction > 0 ? currentZoom - delta : currentZoom + delta;
			if (invert) {
				newZoom = wheel.direction < 0 ? currentZoom - delta : currentZoom + delta;
			}

			if (newZoom > minZoomScale && newZoom < maxZoomScale) {
				viewport.setZoom(newZoom);
				viewport.render();
			}
		}
	}
}

ZoomMouseWheelTool.toolName = "ZoomMouseWheel";
